package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.cartMapper;
import com.sky.service.CartService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private cartMapper cMapper;
    @Autowired
    private DishMapper dMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    public List<ShoppingCart> CheckCart(){
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return cMapper.list(shoppingCart);
    }

    public void addcart(ShoppingCartDTO shoppingCartDTO){
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = cMapper.list(shoppingCart);
        if(list != null && !list.isEmpty()){
            list.get(0).setNumber( list.get(0).getNumber() + 1);
            cMapper.updateNum(list.get(0));
        }
        else{
            shoppingCart.setCreateTime(LocalDateTime.now());
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null){
                //添加的是菜品
                DishVO dish = dMapper.selectById(dishId);
                shoppingCart.setName( dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            else{
                SetmealVO setMeal = setMealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName( setMeal.getName());
                shoppingCart.setImage(setMeal.getImage());
                shoppingCart.setAmount(setMeal.getPrice());
            }
            shoppingCart.setNumber(1);
            cMapper.insert(shoppingCart);
        }
    }

    public void clearAll(){
        Long userId = BaseContext.getCurrentId();
        cMapper.clearAll(userId);
    }

    public void subCart(ShoppingCartDTO shoppingCartDTO){
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = cMapper.list(shoppingCart);
        if(list.get(0).getNumber() > 1){
            list.get(0).setNumber( list.get(0).getNumber() - 1);
            cMapper.updateNum(list.get(0));
        }
        else{
            cMapper.delete(list.get(0));
        }
    }



}
